<template>
  <el-card shadow="always" :body-style="{ padding: '20px' }">
    <template #header>
      <div>
        <el-button
          type="primary"
          size="default"
          icon="ele-Plus"
          @click="offDialog"
          >添加</el-button
        >
      </div>
    </template>
    <!-- card body -->
    <el-table border :stripe="true" :data="records" v-loading="tbLoading">
      <el-table-column type="index" width="70" label="序号" />
      <el-table-column label="品牌名称" prop="tmName" />
      <el-table-column label="品牌logo" v-slot="{ row }">
        <div>
          <img :src="row.logoUrl" alt="" width="100" />
        </div>
      </el-table-column>
      <el-table-column label="操作" v-slot="{ row }">
        <div>
          <el-button
            type="warning"
            size="default"
            icon="ele-Edit"
            @click="updateDialog(row)"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="default"
            icon="ele-Delete"
            @click="deleteTm(row.id)"
            >删除</el-button
          >
        </div>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="prev, pager, next ,sizes,total"
      :total="total"
    />
    <!-- 品牌操作对话框 -->
    <el-dialog v-model="dialogVisible" width="50%" :align-center="true">
      <template #header>
        <div>{{ `${tmInfo.id ? "修改" : "添加"}` }}品牌</div>
      </template>

      <!-- 添加表单 -->
      <el-form label-width="80px">
        <el-form-item label="品牌名称">
          <el-input v-model="tmInfo.tmName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-upload
            class="avatar-uploader"
            :action="`${baseUrl}/admin/product/upload`"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmInfo.logoUrl" :src="tmInfo.logoUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><ele-Plus /></el-icon>
          </el-upload>
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitTm"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "Trademark",
});
</script>

<script setup lang="ts">
import { ref, watch } from "vue";
import {
  reqGetTrademarkInfo,
  type TrademarkListType,
  reqAddTrademark,
  type TrademarkItemType,
  reqUpdataTrademark,
  reqDeleteTrademark,
} from "@/api/product";
import { ElMessage, type UploadProps } from "element-plus";
const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);
const records = ref<TrademarkListType>([]);
const tbLoading = ref(false);

const getTrademarkInfo = async () => {
  tbLoading.value = true;

  const re = await reqGetTrademarkInfo(currentPage.value, pageSize.value);
  total.value = re.total;
  records.value = re.records;

  tbLoading.value = false;
};

watch(
  [currentPage, pageSize],
  () => {
    getTrademarkInfo();
  },
  { immediate: true }
);

// 品牌对话框

const dialogVisible = ref(false);
// const imageUrl = ref("");
const baseUrl = import.meta.env.VITE_API_URL;
// const tmName = ref("");
const tmInfo = ref<TrademarkItemType>({
  tmName: "",
  logoUrl: "",
});

const offDialog = () => {
  dialogVisible.value = true;
  tmInfo.value = {
    tmName: "",
    logoUrl: "",
  };
};

const handleAvatarSuccess: UploadProps["onSuccess"] = (response) => {
  tmInfo.value.logoUrl = response.data;
};

const beforeAvatarUpload: UploadProps["beforeUpload"] = (rawFile) => {
  if (rawFile.type !== "image/jpeg") {
    ElMessage.error("只能传jpg格式图片!");
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("图片大小不能超过2MB!");
    return false;
  }
  return true;
};

const submitTm = async () => {
  if (tmInfo.value.id) {
    await reqUpdataTrademark(tmInfo.value);
    ElMessage.success("修改成功！");
  } else {
    await reqAddTrademark(tmInfo.value);
    ElMessage.success("添加成功！");
  }

  dialogVisible.value = false;
  getTrademarkInfo();
};
const updateDialog = (row: TrademarkItemType) => {
  dialogVisible.value = true;
  tmInfo.value = row;
};
const deleteTm = async (id: number) => {
  await reqDeleteTrademark(id);
  ElMessage.success("删除成功！");
  getTrademarkInfo();
};
</script>

<style scoped lang="scss">
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
